(use-package xelb
  ;;  :load-path "/home/qrthi/documents/projects/elisp/xelb"
  )

(use-package exwm
  ;;  :load-path "/home/qrthi/documents/projects/elisp/exwm"
  :hook
  ;; (exwm-update-class-hook . qrthi/exwm-workspace-rename-buffer)
  (after-init . exwm-enable)
  (exwm-init . qrthi/exwm-frame)
  (exwm-update-title . qrthi/exwm-buffer-name-return-title)
  ;; (exwm-update-title . qrthi/exwm-buffer-name-return-class-name)
  (exwm-manage-finish . qrthi/exwm-video-simulation-keys)
  (exwm-manage-finish . qrthi/exwm-game-simulation-keys)
  ;; (exwm-manage-finish . qrthi/exwm-text-game-simulation-keys)
  (exwm-manage-finish . qrthi/exwm-calibre-simulation-keys)
  :bind
  ([remap async-shell-command] . qrthi/start-process-shell-command)
  ([remap exwm-layout-set-fullscreen] . exwm-layout-toggle-fullscreen)
  ;; ("C-c w g" . exwm-input-send-next-key)
  :custom
  (inhibit-startup-screen t)
  (exwm-manage-force-tiling t)
  (exwm-input-simulation-keys
   '(;; navigation
     ([?\C-a] . [home])
     ([?\C-b] . [left])
     ([?\C-d] . [delete])
     ([?\C-e] . [end])
     ([?\C-f] . [right])
     ([?\C-n] . [down])
     ([?\C-p] . [up])
     ([?\C-v] . [next])
     ([?\M-b] . [C-left])
     ([?\M-f] . [C-right])
     ([?\M-v] . [prior])
     ([?\C-g] . [escape])
     ([?\M-<] . [home])
     ([?\M->] . [end])
     ;; yank/kill
     ;; ([?\C-k] . [S-end delete])
     ([?\C-k] . [S-end ?\C-x])
     ([?\C-w] . [?\C-x])
     ([?\C-y] . [?\C-v])
     ([?\M-d] . [C-S-right ?\C-x])
     ([?\M-h] . [C-S-left ?\C-x])
     ([?\M-w] . [?\C-c])
     ;; editing
     ([?\C-/] . [?\C-z]) ;redo action
     ([?\C-d] . [delete])
     ([?\C-h] . [backspace])
     ([?\C-i] . [tab])
     ([?\C-m] . [return])
     ;; ([C-S-i] . S-tab)
     ([?\M-g?o] . [?f])
     ;; ([?\M-g?O] . [?F])
     ([?\M-g?O] . [?\;?w])
     ([?\M-g?h] . [?\;?h]) ;hint hover
     ([?\M-g?y] . [?\;?y]) ;hint yank
     ([?\M-g?d] . [?\;?d]) ;hint links download
     ([?\C-j] . [?y?p])
     ([?\C-s] . [?\C-f])
     ([?\C-r] . [?N])
     ;; ([?G] . [?R]) ;reload
     ;; ([?L] . [?H]) ;history back
     ;; ([?V] . [?g?f]) ;view-source
     ))
  :init
  (defun qrthi/exwm-workspace-rename-buffer ()
    (exwm-workspace-rename-buffer exwm-class-name))
  (defun qrthi/exwm-frame ()
    (menu-bar-mode -1)
    (tool-bar-mode -1)
    (scroll-bar-mode -1)
    (fringe-mode 0)
    (blink-cursor-mode 0)
    )
  (defun qrthi/start-process-shell-command (command)
    (interactive (list (read-shell-command "$ ")))
    (start-process-shell-command command nil command))
  (defun qrthi/exwm-buffer-name-return-title ()
    (exwm-workspace-rename-buffer exwm-title))
  (defun qrthi/exwm-buffer-name-return-class-name ()
    (exwm-workspace-rename-buffer exwm-class-name))
  (defun qrthi/exwm-return-title ()
    (interactive)
    (message exwm-title))
  (defun qrthi/exwm-return-class-name ()
    (interactive)
    (message exwm-class-name))
  (defun qrthi/exwm-video-simulation-keys ()
    (when (and exwm-class-name
	       (or (string= exwm-class-name "mpv")
		   (string= exwm-class-name "mplayer")))
      (exwm-input-set-local-simulation-keys
       '(([?\C-m] . [pause])
	 ([?\C-b] . [left])
	 ([?\C-f] . [right])
	 ([?\C-p] . [down])
	 ([?\C-n] . [up])
	 ([?\M-v] . [prior])
	 ([?\C-v] . [next])
	 ;; ([?\M-<] . [S-down])
	 ;; ([?\M->] . [S-up])
	 ;; ([?r] . [M-right]) ;rotate 90°
	 ([?>] . [?>]) ;playlist-next
	 ([?<] . [?<])
	 ([?\M-<] . [?\{]) ;speed 0.5
	 ([?\M->] . [?\}]) ;speed 2
	 ([?.] . [?.]) ;frame-step
	 ([?,] . [?,]) ;frame-back-step
	 ([?o] . [?o]) ;progress
	 ;; ([?i] . [?i]) ;stats
	 ;; ([?d] . [delete])
	 ;; ([?n] . [?>]) ;next vide
	 ([?p] . [?<]) ;previous video
	 ([?n] . [?>])
	 ([?l] . [?L]) ;loop inf
	 ([?L] . [?l]) ;a-b loop
	 ([?s] . [?J]) ;cycle sub
	 ([?m] . [?m]) ;mute
	 ([?q] . [?Q]) ;quit-watch-later
	 ([?Q] . [?q])
	 ([?S] . [?s]) ;screenshot
	 ;; ([?p] . o)			;show progress
	 ))))
  (defun qrthi/exwm-calibre-simulation-keys ()
    (when (and exwm-class-name
	       (string= exwm-class-name "calibre"))
      (exwm-input-set-local-simulation-keys
       '(([?>] . [?\C-f])
	 ([?<] . [?\C-b])
	 ([?\C-f] . [right])
	 ([?f] . [right])
	 ([?\C-b] . [left])
	 ([?b] . [left])
	 ([?\C-n] . [down])
	 ([?n] . [down])
	 ([?\C-p] . [up])
	 ([?p] . [up])
	 ([?\C-v] . [next])
	 ([?\M-v] . [prior])
	 ([?\C-d] . [delete])
	 ([?\M-<] . [home])
	 ([?\M->] . [end])
	 ([?\C-g] . [escape])
	 ([?\C-m] . [return])
	 ([?\C-s] . [?\C-f])
	 ([?v] . [?\M-v])
	 ;; ([?E] . [\f12]) ;edit metadata
	 ([?a] . [?A]) ;add books
	 ([?d] . [?\C-d]) ;download metadata
	 ([?i] . [?i]) ;show details
	 ([?k] . [?K]) ;edit table of contents
	 ([?c] . [?C]) ;convert
	 ([?s] . [?d]) ;send to device
	 ;; ([?D] . [?O]) ;open containing folder
	 ))))
  (defun qrthi/exwm-game-simulation-keys ()
    (when (and exwm-title
	       (string= exwm-title "Hotline Miami"))
      (exwm-input-set-local-simulation-keys
       '(;; ([?c] . [up])
	 ;; ([?t] . [down])
	 ;; ([?h] . [left])
	 ;; ([?n] . [right])
	 ([?\C-m] . [return])
	 ([?\C-g] . [escape])
	 ([?,] . [?w])
	 ([?o] . [?s])
	 ([?e] . [?d])
	 ;; ([?q] . [?x])
	 ;; ([?'] . [?z])
	 ;; ([?a] . [?a])
	 ;; ([?o] . [?s])
	 ;; ([?,] . [?q])
	 ;; ([?.] . [?w])
	 ;; ([?e] . [space])
	 ))))
  (defun qrthi/exwm-text-game-simulation-keys ()
    (when (and exwm-class-name
	       (string= exwm-class-name "coc2electron"))
      (exwm-input-set-local-simulation-keys
       '(;; ([?\;] . [1])
	 ;; ([?,] . [2])
	 ;; ([?.] . [3])
	 ;; ([?p] . [4])
	 ;; ([?y] . [5])
	 ([?a] . [?q])
	 ([?o] . [?w])
	 ([?u] . [?r])
	 ([?i] . [?t])
	 ([?'] . [?a])
	 ([?q] . [?s])
	 ([?j] . [?d])
	 ([?k] . [?f])
	 ([?n] . [down])
	 ([?p] . [up])
	 ))))

  )

(use-package exwm-systemtray
  :hook (after-init . exwm-systemtray-enable)
  :custom
  (exwm-systemtray-height 14))
